//
//  NetWorkModal.m
//  ClassifyShop
//
//  Created by yuyao on 15/3/21.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "NetWorkModal.h"

@implementation NetWorkModal
@synthesize delegate;
static NetWorkModal *sharedNetWorkModalInstance = nil;
static AFHTTPRequestOperationManager *manager = nil;

+ (NetWorkModal *)sharedInstance
{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedNetWorkModalInstance = [[self alloc] init];
        manager = [AFHTTPRequestOperationManager manager];
    });

    return sharedNetWorkModalInstance;
}

- (void)getRestaurantListWithKeyWord:(NSString *)keyword andPageNumber:(int)pageNumber{
    
    NSString *urlString =[NSString  stringWithFormat:@"http://101.227.251.228:8081/shop/m/restaurant/list?searchProperty=keyword&searchValue=%@&pageNumber=%d",keyword,pageNumber];
    NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    [manager GET:encodingString  parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *datas = [NSMutableArray array];
       // NSLog(@"description＝: %@", [responseObject objectForKey:@"data"]);

        if ([[responseObject objectForKey:@"success"] intValue]==1) {
            NSLog(@"count＝: %lu", (unsigned long)[[[responseObject objectForKey:@"data"] objectForKey:@"content"] count]);
            NSLog(@"total＝: %d", [[[responseObject objectForKey:@"data"] objectForKey:@"total"] intValue]);
            NSLog(@"totalPages＝%d", [[[responseObject objectForKey:@"data"] objectForKey:@"totalPages"] intValue]);
            
            for (NSDictionary *dic in [[responseObject objectForKey:@"data"] objectForKey:@"content"]) {
              //  NSLog(@"name = %@",[dic objectForKey:@"name"]);
                
                
                RestaurantCore *rcore = [[RestaurantCore alloc] initWithRestId:[[dic objectForKey:@"id"] intValue]
                                                                          Name:[dic objectForKey:@"name"]
                                                                         Thumb:[dic objectForKey:@"thumb"]
                                                                  Introduction:[dic objectForKey:@"introduction"]
                                                                     Recommend:[dic objectForKey:@"recommend"]
                                                                       Address:[dic objectForKey:@"addr"]
                                                                          Logo:[dic objectForKey:@"logo"]
                                                                         Order:[[dic objectForKey:@"order"] intValue]
                                                                       Keyword:[dic objectForKey:@"keyword"]
                                                                      FoodsArray:[dic objectForKey:@"foods"]];
                
                
                
                
                [datas addObject:rcore];
            }
            if ([delegate respondsToSelector:@selector(getRestaurantListDelegateMethod:andPagesize:)]) {
                [delegate getRestaurantListDelegateMethod:datas andPagesize:[[[responseObject objectForKey:@"data"] objectForKey:@"totalPages"] intValue]];

            }

            
        }else{
            
            //[delegate getRestaurantListDelegateMethod:nil andPagesize:0];

        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [delegate getRestaurantListDelegateMethod:nil andPagesize:0];
        NSLog(@"Error: %@", error);
        
    }];
}
- (void)getRestaurantWithRestId:(int)rid{
    NSString *urlString =[NSString  stringWithFormat:@"http://101.227.251.228:8081/shop/m/restaurant/view/%d",rid];
    NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [manager GET:encodingString  parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
         NSLog(@"description＝: %@", [responseObject objectForKey:@"data"]);
        
        if ([[responseObject objectForKey:@"success"] intValue]==1) {
            NSLog(@"count＝: %lu", (unsigned long)[[[responseObject objectForKey:@"data"] objectForKey:@"content"] count]);
            NSLog(@"total＝: %d", [[[responseObject objectForKey:@"data"] objectForKey:@"total"] intValue]);
            NSLog(@"totalPages＝%d", [[[responseObject objectForKey:@"data"] objectForKey:@"totalPages"] intValue]);
            
            NSDictionary *dic = [responseObject objectForKey:@"data"];
                
            
            RestaurantCore *rcore = [[RestaurantCore alloc] initWithRestId:[[dic objectForKey:@"id"] intValue]
                                                                          Name:[dic objectForKey:@"name"]
                                                                         Thumb:[dic objectForKey:@"thumb"]
                                                                  Introduction:[dic objectForKey:@"introduction"]
                                                                     Recommend:[dic objectForKey:@"recommend"]
                                                                       Address:[dic objectForKey:@"addr"]
                                                                          Logo:[dic objectForKey:@"logo"]
                                                                         Order:[[dic objectForKey:@"order"] intValue]
                                                                       Keyword:[dic objectForKey:@"keyword"]
                                                                      FoodsArray:[dic objectForKey:@"foods"]];
                
                
            
            

            [delegate getRestaurant:rcore];
            
            
        }else{
            
            //[delegate getRestaurantListDelegateMethod:nil andPagesize:0];
            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [delegate getRestaurant:nil];
        NSLog(@"Error: %@", error);
        
    }];
}
@end
